﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
using System.IO;
using System.Text.RegularExpressions;

namespace Payroll
{
    public partial class PaiedForm : Form
    {
        public PaiedForm()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void PayedForm_Load(object sender, EventArgs e)
        {
            string connString = @"Data Source=..\..\Database1.sdf";
            SqlCeConnection connection = new SqlCeConnection(connString);
            connection.Open();

            string sqlStr = "select * from payemployee order by payDate";
            DataSet dataSet = new DataSet();
            SqlCeDataAdapter sqldata = new SqlCeDataAdapter(sqlStr, connString);
            sqldata.Fill(dataSet, "payemployee");
            DataView dataView = new DataView(dataSet.Tables["payemployee"]);
            dataGridView1.DataSource = dataView;

            myFile.myWrite_operate(myUser.Name, "查看工资支付记录");
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connString = @"Data Source=..\..\Database1.sdf";
            SqlCeConnection connection = new SqlCeConnection(connString);
            connection.Open();

            string sqlStr = "delete  from Payemployee";
            SqlCeCommand cmd = new SqlCeCommand(sqlStr, connection);
            
            cmd.ExecuteNonQuery();
            MessageBox.Show("删除成功！");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DownloadPaied f = new DownloadPaied();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();
            string filename = ofd.FileName;

            if(filename != "")
            {
                if (Regex.IsMatch(filename, "[A-Za-z0-9_\u4e00-\u9fa5]-encryption.txt$"))//如果打开的是加密过的文件，提示是否需要解密
                {
                    DialogResult dr = MessageBox.Show("该文件已被加密，是否需要进行解密?", "提示", MessageBoxButtons.OKCancel);

                    if (dr == DialogResult.OK)
                    {
                        Cipher.Decryption(filename);
                        filename = filename.Insert(filename.LastIndexOf('.'), "-decryption");
                    }
                }

                textBox1.Visible = true;
                textBox1.Text = File.ReadAllText(filename);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            PaiedLINQ f = new PaiedLINQ();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.Show();
        }
    }
}
